package reflect;

import java.io.File;
import java.lang.reflect.Field;

public class ReflectDemo8 {
    public static void main(String[] args) throws Exception {
        Class cls = Class.forName("reflect.Person");
        Object obj = cls.newInstance();

        Field filed = cls.getDeclaredField("name");
        filed.setAccessible(true);
        filed.set(obj,"张三");
        String name = (String)filed.get(obj);
        System.out.println("姓名:"+name);
        filed.setAccessible(false);
        System.out.println(obj);

        Field filed1 = cls.getDeclaredField("age");
        filed1.setAccessible(true);
        filed1.set(obj,30);
        int age = (int)filed1.get(obj);
        System.out.println("年龄:"+age);
        filed1.setAccessible(false);
        System.out.println(obj);


    }
}
